exports.register = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;

    //最终结果
    var resultData = {};

    //注册后的用户ID
    var userId = 0;

    //任务流创建
    async.waterfall([

        //查询该用户是否已经存在
        function(cb){
            
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.userSearch + "/u_username/" + bodyParams.username,
                    method: 'GET'
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return RES.response(res, false, data);
                    }

                    if(data.data.length > 0){
                        return RES.response(res, false, "抱歉，该手机号已经注册过了");
                    }
                    
                    cb();
                    
                }
            );

        },
            
        //验证短信码是否正确
        function(cb) {


            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.validationSmsCode,
                    method: 'POST',
                    data: {
                        phone : bodyParams.username,
                        code  : bodyParams.code
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return RES.response(res, false, data);
                    }
                    
                     cb();
                    
                }
            );

         

        },

        //注册用户
        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.userAdd,
                    method: 'POST',
                    data: {
                        u_username  : bodyParams.username,
                        u_password  : bodyParams.password,
                        u_nickname  : bodyParams.username.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
                    }
                },
                function (err, data) {
                    //判断是否有错误信息
                    if (err) {
                        return RES.response(res, false, data);
                    }

                    userId = data;
                    
                    cb();
                    
                }
            );

        },

        //返回注册后的信息
        function(cb){

            resultData.userid   = userId;
            resultData.username = bodyParams.username;
            resultData.nickname = bodyParams.username.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
            
            cb();

        }

    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, resultData);

    });

}